package com.qdsgjsfk.vision.util;

import android.annotation.SuppressLint;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 屈光页面数据检查和美沃验光仪数据转换类
 *
 * @time 2020/12/15 9:22
 * Created by lbj on 2020/12/15 9:22.
 */
public class DiopterDataUtil {

    public static boolean checkR1MM(EditText odR1mm, String odR1m) {
        if (!UtilString.isEmpty( odR1m )) {
            return !(Double.parseDouble( odR1m ) < 5) && !(Double.parseDouble( odR1m ) > 10);
        } else {
            return true;
        }
    }

    public static boolean checkR1D(EditText odR1mm, String odR1m) {
        if (!UtilString.isEmpty( odR1m )) {
            return !(Double.parseDouble( odR1m ) < 33.75) && !(Double.parseDouble( odR1m ) > 67.50);
        } else {
            return true;
        }
    }

    public static boolean checkAX(EditText odAX, String odAxx) {
        if (!UtilString.isEmpty( odAxx )) {
            return !(Double.parseDouble( odAxx ) < 0) && !(Double.parseDouble( odAxx ) > 180);
        } else {
            return true;
        }
    }


    public static List<String> initListItemsOne() {
        List<String> optionsItems = new ArrayList<String>();
        for (int i = 0; i < 21; i++) {
            optionsItems.add( "+" + i );
//            if (i < 10) {
//                optionsItems.add( "+0" + i );
//            } else {
//                optionsItems.add( "+" + i );
//            }
        }
        for (int i = 0; i < 21; i++) {
            optionsItems.add( "-" + i );
//            if (i < 10) {
//                optionsItems.add( "-0" + i );
//            } else {
//                optionsItems.add( "-" + i );
//            }
        }
        return optionsItems;

    }

    public static List<String> initListItemsTwo() {
        List<String> optionsItems = new ArrayList<String>();
        optionsItems.add( ".00" );
        optionsItems.add( ".25" );
        optionsItems.add( ".50" );
        optionsItems.add( ".75" );
        return optionsItems;

    }

    public static List<String> initListItemsThree() {
        List<String> optionsItems = new ArrayList<String>();

        for (int i = 0; i < 21; i++) {
            optionsItems.add( "-" + i );
//            if (i < 10) {
//                optionsItems.add( "-0" + i );
//            } else {
//                optionsItems.add( "-" + i );
//            }
        }
        return optionsItems;
    }

    /**
     * 去除字符串中的空格和换行
     *
     * @time 2021/3/25 16:58
     */
    private static String replace(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile( "\\s*|\t|\r|\n" );
            Matcher m = p.matcher( str );
            dest = m.replaceAll( "" );
        }
        return dest;
    }

    //去掉一个字符串中的换行符、回车符等，将连续多个空格替换成一个空格
    public static String replaceString(String string) {
        String dest = "";
        Pattern p = Pattern.compile( "[\t\r\n]" );
        Matcher m = p.matcher( string );
        string = m.replaceAll( "" );
        string = string.replaceAll( " +", " " );
        dest = string;
        return dest;
    }


    private static String subString(String str, String strStart, String strEnd) {

        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf( strStart );
        int strEndIndex = str.indexOf( strEnd );

        /* index 为负数 即表示该字符串中 没有该字符 */
        if (strStartIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strStart + ", 无法截取目标字符串";
        }
        if (strEndIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strEnd + ", 无法截取目标字符串";
        }
        /* 开始截取 */
        return str.substring( strStartIndex, strEndIndex ).substring( strStart.length() );
    }

    private static String subStringOne(String str, String strStart, String strEnd) {

        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf( strStart );
        int strEndIndex = str.lastIndexOf( strEnd );

        /* index 为负数 即表示该字符串中 没有该字符 */
        if (strStartIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strStart + ", 无法截取目标字符串";
        }
        if (strEndIndex < 0) {
            return "字符串 :---->" + str + "<---- 中不存在 " + strEnd + ", 无法截取目标字符串";
        }
        /* 开始截取 */
        return str.substring( strStartIndex, strEndIndex ).substring( strStart.length() );
    }

    //获取右眼球镜柱镜轴位
    public static List<String> getListRight(String string) {
        String rightStrting = subString( string, "R: S C A ", " L: S C A" );
        return new ArrayList<>( Arrays.asList( rightStrting.split( " " ) ) );
    }

    //获取左眼球镜柱镜轴位
    public static List<String> getListLeft(String string) {
        String subString = subStringOne( string, "L: S C A ", " S.E." );
        return new ArrayList<>( Arrays.asList( subString.split( " " ) ) );
    }

    //获取右眼角膜曲率
    public static List<String> getListRightRA(String string) {
        String rightStrting = subString( string, "R: D MM A R1 ", " L: D MM A R1" );
        return new ArrayList<>( Arrays.asList( rightStrting.split( " " ) ) );
    }

    //获取左眼角膜曲率
    public static List<String> getListLeftRA(String string) {
        String rightStrting = subStringOne( string, "L: D MM A R1 ", " CYL:" );
        return new ArrayList<>( Arrays.asList( rightStrting.split( " " ) ) );
    }

    //只有左眼或者只有右眼的情况
    //获取右眼球镜柱镜轴位
    public static List<String> getListRightOnly(String string) {
        String rightStrting = subString( string, "R: S C A ", " S.E." );
        return new ArrayList<>( Arrays.asList( rightStrting.split( " " ) ) );
    }

    //只有左眼或者只有右眼的情况
    //获取右眼球镜柱镜轴位
    public static List<String> getListLeftOnly(String string) {
        String rightStrting = subString( string, "L: S C A ", " S.E." );
        return new ArrayList<>( Arrays.asList( rightStrting.split( " " ) ) );
    }

    //获取右眼角膜曲率
    public static List<String> getListRightRAOnly(String string) {
        String rightStrting = subString( string, "R: D MM A R1 ", " CYL:" );
        return new ArrayList<>( Arrays.asList( rightStrting.split( " " ) ) );
    }

    //获取左眼角膜曲率
    public static List<String> getListLeftRAOnly(String string) {
        String rightStrting = subString( string, "L: D MM A R1 ", " CYL:" );
        return new ArrayList<>( Arrays.asList( rightStrting.split( " " ) ) );
    }

    //设置尼德克AR-1设备球镜柱镜轴位
    public static void setNIDEKAR1(String data, EditText odSph, EditText odCyl, EditText odAxial, EditText osSph, EditText osCyl, EditText osAxial) {
        if (data.contains( "OL" ) && data.contains( "OR" )) {//左右眼都有值
            String listAllRight = data.substring( data.indexOf( "OR" ) + 2, data.indexOf( "OR" ) + 17 );
            String listAllLeft = data.substring( data.indexOf( "OL" ) + 2, data.indexOf( "OL" ) + 17 );
            odSph.setText( listAllRight.substring( 0, 6 ) );
            odCyl.setText( listAllRight.substring( 6, 12 ) );
            odAxial.setText( listAllRight.substring( 12, 15 ) );
            osSph.setText( listAllLeft.substring( 0, 6 ) );
            osCyl.setText( listAllLeft.substring( 6, 12 ) );
            osAxial.setText( listAllLeft.substring( 12, 15 ) );
        } else if (data.contains( "OR" )) {//右眼有值
            String listRight = data.substring( data.indexOf( "OR" ) + 2, data.indexOf( "OR" ) + 17 );
            odSph.setText( listRight.substring( 0, 6 ) );
            odCyl.setText( listRight.substring( 6, 12 ) );
            odAxial.setText( listRight.substring( 12, 15 ) );
        } else if (data.contains( "OL" )) {//左眼有值
            String listLeft = data.substring( data.indexOf( "OL" ) + 2, data.indexOf( "OL" ) + 17 );
            osSph.setText( listLeft.substring( 0, 6 ) );
            osCyl.setText( listLeft.substring( 6, 12 ) );
            osAxial.setText( listLeft.substring( 12, 15 ) );
        }
    }

    //设置雄博AXL-800设备眼轴数据
    @SuppressLint("SetTextI18n")
    public static void setAXL800(String data, EditText odAl, EditText odVar, EditText odK1, EditText odK2, EditText odAvgk, EditText odAlav, EditText osAl, EditText osVar, EditText osK1, EditText osK2, EditText osAvgk, EditText osAlav) {
        List<String> list = new ArrayList<>( Arrays.asList( data.split( "\\|\\|" ) ) );
        odAl.setText( list.get( 0 ).substring( list.get( 0 ).indexOf( "-" ) + 1 ) );
        odVar.setText( list.get( 1 ).substring( list.get( 1 ).indexOf( "-" ) + 1 ) );
        String odK1Data = list.get( 2 );
        if (odK1Data.contains( "@" )) {
            odK1.setText( odK1Data.substring( odK1Data.indexOf( "-" ) + 1, odK1Data.indexOf( "mm" ) ) + odK1Data.substring( odK1Data.indexOf( "/" ), odK1Data.indexOf( "@" ) ) );
        }
        String odK2Data = list.get( 3 );
        if (odK2Data.contains( "@" )) {
            odK2.setText( odK2Data.substring( odK2Data.indexOf( "-" ) + 1, odK2Data.indexOf( "mm" ) ) + odK2Data.substring( odK2Data.indexOf( "/" ), odK2Data.indexOf( "@" ) ) );
        }
        if (list.size() == 13) {
            String odAvgkData = list.get( 5 );
            if (odAvgkData.contains( "mm" )) {
                odAvgk.setText( odAvgkData.substring( odAvgkData.indexOf( "K-" ) + 2, odAvgkData.indexOf( "mm" ) ) + odAvgkData.substring( odAvgkData.indexOf( "/" ) ) );
            }
            odAlav.setText( list.get( 6 ).substring( list.get( 6 ).indexOf( "K-" ) + 2, list.get( 6 ).indexOf( "<OS/L>" ) ) );
            osAl.setText( list.get( 6 ).substring( list.get( 6 ).indexOf( "AL-" ) + 3 ) );//左眼AL
            osVar.setText( list.get( 7 ).substring( list.get( 7 ).indexOf( "-" ) + 1 ) );
            String osK1Data = list.get( 8 );
            if (osK1Data.contains( "@" )) {
                osK1.setText( osK1Data.substring( osK1Data.indexOf( "-" ) + 1, osK1Data.indexOf( "mm" ) ) + osK1Data.substring( osK1Data.indexOf( "/" ), osK1Data.indexOf( "@" ) ) );
            }
            String osK2Data = list.get( 9 );
            if (osK2Data.contains( "@" )) {
                osK2.setText( osK2Data.substring( osK2Data.indexOf( "-" ) + 1, osK2Data.indexOf( "mm" ) ) + osK2Data.substring( osK2Data.indexOf( "/" ), osK2Data.indexOf( "@" ) ) );
            }
            String osAvgkData = list.get( 11 );
            if (osAvgkData.contains( "mm" )) {
                osAvgk.setText( osAvgkData.substring( osAvgkData.indexOf( "K-" ) + 2, osAvgkData.indexOf( "mm" ) ) + osAvgkData.substring( osAvgkData.indexOf( "/" ) ) );
            }
            osAlav.setText( list.get( 12 ).substring( list.get( 12 ).indexOf( "K-" ) + 2 ) );
        } else if (list.size() == 11) {
            String odAvgkData = list.get( 4 );
            if (odAvgkData.contains( "mm" )) {
                odAvgk.setText( odAvgkData.substring( odAvgkData.indexOf( "K-" ) + 2, odAvgkData.indexOf( "mm" ) ) + odAvgkData.substring( odAvgkData.indexOf( "/" ) ) );
            }
            odAlav.setText( list.get( 5 ).substring( list.get( 5 ).indexOf( "K-" ) + 2, list.get( 5 ).indexOf( "<OS/L>" ) ) );
            osAl.setText( list.get( 5 ).substring( list.get( 5 ).indexOf( "AL-" ) + 3 ) );//左眼AL
            osVar.setText( list.get( 6 ).substring( list.get( 6 ).indexOf( "-" ) + 1 ) );
            String osK1Data = list.get( 7 );
            if (osK1Data.contains( "@" )) {
                osK1.setText( osK1Data.substring( osK1Data.indexOf( "-" ) + 1, osK1Data.indexOf( "mm" ) ) + osK1Data.substring( osK1Data.indexOf( "/" ), osK1Data.indexOf( "@" ) ) );
            }
            String osK2Data = list.get( 8 );
            if (osK2Data.contains( "@" )) {
                osK2.setText( osK2Data.substring( osK2Data.indexOf( "-" ) + 1, osK2Data.indexOf( "mm" ) ) + osK2Data.substring( osK2Data.indexOf( "/" ), osK2Data.indexOf( "@" ) ) );
            }
            String osAvgkData = list.get( 9 );
            if (osAvgkData.contains( "mm" )) {
                osAvgk.setText( osAvgkData.substring( osAvgkData.indexOf( "K-" ) + 2, osAvgkData.indexOf( "mm" ) ) + osAvgkData.substring( osAvgkData.indexOf( "/" ) ) );
            }
            osAlav.setText( list.get( 10 ).substring( list.get( 10 ).indexOf( "K-" ) + 2 ) );
        }

    }

    //设置CVSX 视力筛选仪 目乐医疗设备球镜柱镜轴位
    public static void setCVSX(byte[] data, EditText odSph, EditText odCyl, EditText odAxial, EditText osSph, EditText osCyl, EditText osAxial) {
        byte[] bytes = new byte[8];
        byte[] bOdSph = new byte[8];
        byte[] bOsCyl = new byte[8];
        byte[] bOdCyl = new byte[8];
        byte[] bOsAxial = new byte[8];
        byte[] bOdAxial = new byte[8];
        System.arraycopy( data, 32, bytes, 0, 8 );  //数组截取
        System.arraycopy( data, 40, bOdSph, 0, 8 );  //数组截取
        System.arraycopy( data, 48, bOsCyl, 0, 8 );  //数组截取
        System.arraycopy( data, 56, bOdCyl, 0, 8 );  //数组截取
        System.arraycopy( data, 80, bOsAxial, 0, 8 );  //数组截取
        System.arraycopy( data, 88, bOdAxial, 0, 8 );  //数组截取
        osSph.setText( new String( bytes ) );
        odSph.setText( new String( bOdSph ) );
        osCyl.setText( new String( bOsCyl ) );
        odCyl.setText( new String( bOdCyl ) );
        osAxial.setText( new String( bOsAxial ) );
        odAxial.setText( new String( bOdAxial ) );
        String ss = new String( bytes );
        Log.e( "bleDataTemp", "******:" + ss );
    }


    //设置拓普康KR-800设备球镜柱镜轴位
    public static void setKR800(String data, EditText odSph, EditText odCyl, EditText odAxial, EditText osSph, EditText osCyl, EditText osAxial) {
        if (data.contains( "OAR" ) && data.contains( "OAL" )) {//左右眼都有值
            String rightStrting = subString( replace( data ), "OAR", "EAR" );
            String leftStrting = subString( replace( data ), "OAL", "EAL" );
            if (rightStrting.length() == 12) {
                odSph.setText( rightStrting.substring( 0, 5 ).replace( " ", "" ) );
                odCyl.setText( rightStrting.substring( 5, 10 ).replace( " ", "" ) );
                odAxial.setText( rightStrting.substring( 10 ).replace( " ", "" ) );
            }
            if (leftStrting.length() == 12) {
                osSph.setText( leftStrting.substring( 0, 5 ).replace( " ", "" ) );
                osCyl.setText( leftStrting.substring( 5, 10 ).replace( " ", "" ) );
                osAxial.setText( leftStrting.substring( 10 ).replace( " ", "" ) );
            }

        } else if (data.contains( "OAR" )) {//右眼有值
            String rightStrting = subString( replace( data ), "OAR", "EAR" );
            if (rightStrting.length() == 12) {
                odSph.setText( rightStrting.substring( 0, 5 ).replace( " ", "" ) );
                odCyl.setText( rightStrting.substring( 5, 10 ).replace( " ", "" ) );
                odAxial.setText( rightStrting.substring( 10 ).replace( " ", "" ) );
            }
        } else if (data.contains( "OAL" )) {//左眼有值
            String leftStrting = subString( replace( data ), "OAL", "EAL" );
            if (leftStrting.length() == 12) {
                osSph.setText( leftStrting.substring( 0, 5 ).replace( " ", "" ) );
                osCyl.setText( leftStrting.substring( 5, 10 ).replace( " ", "" ) );
                osAxial.setText( leftStrting.substring( 10 ).replace( " ", "" ) );
            }
        }
    }

}
